package com.libinyn.base.controller;


import com.libinyn.base.model.sys.SysUserEntity;
import com.libinyn.base.service.system.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.List;


/**
 * 主页跳转
 * @fileName: MainController.java
 * @author: bin.li01
 * @date: 2017/11/28 18:13
 * @version: v1.0.0
 */
@Controller
@RequestMapping("/")
public class MainController {

	private static final Logger logger = LoggerFactory.getLogger(MainController.class);

	@Resource
	private ISysUserService sysUserService;

	@RequestMapping
	public String index(ModelMap modelMap){
		modelMap.put("name","libin");
		return "index";
	}

	@RequestMapping(name = "/login", method = RequestMethod.POST)
	public String login(ModelMap modelMap, SysUserEntity sysUserEntity){
		SysUserEntity sysUser= new SysUserEntity();
		sysUser.setUsername(sysUserEntity.getUsername());
		List<SysUserEntity> users;
		try {
			 users = sysUserService.select(sysUser);
			if (!CollectionUtils.isEmpty(users)){
				sysUser = users.get(0);
			}

			if (sysUser == null){
				modelMap.put("errorMsg","用户为空");
				return "error";
			}

		} catch (Exception e) {
			logger.error("查询数据异常！",e);
			modelMap.put("errorMsg","查询数据异常");
			return "error";
		}
		modelMap.put("sysUser",sysUser);
		return "main";
	}


}
